package cn.kxx.serviceImpl;

import cn.kxx.config.DateUtil;
import cn.kxx.exception.BusinessException;
import cn.kxx.mapper.MemberMapper;
import cn.kxx.mapper.OrderMapper;
import cn.kxx.mapper.SetmealMapper;
import cn.kxx.pojo.Member;
import cn.kxx.service.ReportService;
import cn.kxx.vo.BusinessReportVo;
import cn.kxx.vo.HotSetmealVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;


    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 获得统计数据报告报告
     *
     * @return {@link BusinessReportVo}
     * @throws BusinessException
     */
    @Override
    public BusinessReportVo getBusinessReport() throws BusinessException {


        BusinessReportVo businessReportVo = new BusinessReportVo();
// 获取统计日期
        Date nowData = new Date();

        businessReportVo.setReportDate(nowData);


//        获取当天新增会员
        String nowStr = DateUtil.dateToStr(nowData, "yyyy-MM-dd");

        Integer count = memberMapper.selectCount(new LambdaQueryWrapper<Member>().eq(Member::getRegTime, nowStr));
        businessReportVo.setTodayNewMember(count);

//        获取总会员数
        Integer count1 = memberMapper.selectCount(null);
        businessReportVo.setTotalMember(count1);

//        获取本周新增会员数
        int thisWeekNewMember = memberMapper.getThisWeekNewMember();
        businessReportVo.setThisWeekNewMember(thisWeekNewMember);

//       获取本月新增会员数
        int thisMonthNewMember = memberMapper.getThisMonthNewMember();
        businessReportVo.setThisMonthNewMember(thisMonthNewMember);


//       当天与约束
        int todayOrderNumber = orderMapper.getTodayOrderNumber(false);
        businessReportVo.setTodayOrderNumber(todayOrderNumber);
        //todayVisitsNumber当天到诊人数
        int todayVisitsNumber = orderMapper.getTodayOrderNumber(true);
        businessReportVo.setTodayVisitsNumber(todayVisitsNumber);

        //thisWeekOrderNumber本周预约数
        int thisWeekOrderNumber = orderMapper.getThisWeekOrderNumber(false);
        businessReportVo.setThisWeekOrderNumber(thisWeekOrderNumber);

        //thisWeekVisitsNumber本周到诊数
        int thisWeekVisitsNumber = orderMapper.getThisWeekOrderNumber(true);
        businessReportVo.setThisWeekVisitsNumber(thisWeekVisitsNumber);

        //thisMonthOrderNumber本月预约数
        int thisMonthOrderNumber = orderMapper.getThisMonthOrderNumber(false);
        businessReportVo.setThisMonthOrderNumber(thisMonthOrderNumber);

        //thisMonthVisitsNumber本月到诊数
        int thisMonthVisitsNumber = orderMapper.getThisMonthOrderNumber(true);
        businessReportVo.setThisMonthVisitsNumber(thisMonthVisitsNumber);

        //获取热门套餐
        List<HotSetmealVo> hotSetMeals = setmealMapper.getHotSetMeals();
        log.info("============获取到的热门套餐是:" + hotSetMeals);
        businessReportVo.setHotSetmeal(hotSetMeals);


        return businessReportVo;
    }
}
